package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", String.class, getUserId());
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", this.getUserId(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);

    }

    private String getUserId(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null){
            String userId = loginUser.getUserId().toString();
            return userId;
        }
        return "0";
    }
}
